package service.impl;

import dao.UserDao;
import dao.impl.UserDaoImpl;
import domain.Article;
import domain.Favour;
import domain.PageBean;
import domain.User;
import service.UserService;
import util.MailUtils;
import util.UuidUtil;

import java.util.List;


public class UserServiceImpl implements UserService {

    private UserDao userDao = new UserDaoImpl();

    /**
     * 注册用户
     *
     * @param user
     * @param createTime
     * @return
     */
    @Override
    public boolean regist(User user, String createTime) {
        //1.根据用户名查询用户对象
        User u = userDao.findByUsername(user.getUsername());
        //判断u是否为null
        if (u != null) {
            //用户名存在，注册失败
            return false;
        }
        //2.保存用户信息
        //2.1设置激活码，唯一字符串
        user.setCode(UuidUtil.getUuid());
        //2.2设置激活状态
        user.setStatus("N");
        userDao.save(user, createTime);
        System.out.println(user + "----UserServiceImp----regist");
        //3.激活邮件发送，邮件正文？
        //这里的activeUserServlet?code= 是为了将生成的code发送给用户邮箱,当用户点击链接激活的时候
        //ActiveUserServlet 这个类再次接收code,根据这个code修改用户状态为已激活
        String content = "<a href='http://localhost:8080/user/active?code=" + user.getCode() + "'<a>点击激活【文墨社区】</a>";

        MailUtils.sendMail(user.getEmail(), content, "激活账号");

        return true;
    }

    /**
     * 激活用户
     *
     * @param code
     * @return
     */
    @Override
    public boolean active(String code) {
        //1.根据激活码查询用户对象
        User user = userDao.findByCode(code);
        if (user != null) {
            //2.调用dao的修改激活状态的方法
            userDao.updateStatus(user);
            return true;
        } else {
            return false;
        }
    }

    /**
     * 登录方法
     *
     * @param user
     * @return
     */
    @Override
    public User login(User user) {
        return userDao.findByUsernameAndPassword(user.getUsername(), user.getPassword());
    }

    @Override
    public User findOneByCode(String code) {
        User user = userDao.findByCode(code);
        return user;
    }

    @Override
    public int findArticleNumByCode(String code) {
        return userDao.findArticleNum(code);
    }

    @Override
    public int updateNickNameByCode(String code, String nickname) {
        return userDao.updateNickNameByCode(code, nickname);
    }

    @Override
    public int updatePasswordByCode(String code, String newPassword) {
        return userDao.updatePasswordByCode(code, newPassword);
    }

    @Override
    public Article setArticle(int cid, String visible, String title, String text, String createTime, String code) {
        //先添加数据
        userDao.setArticle(cid, visible, title, text, createTime, code);
        //数据添加完成之后再次根据code把这个文章对象返回
        Article article = userDao.findArticleByCode(code, createTime);
        return article;
    }

    @Override
    public int setLabel(String code, int rid, String[] tags) {
        int row = 0;
        //如果一个标签都没有选,不执行添加标签操作
        if (tags.length == 0 || tags == null) {
            return 0;
        }
        // 标签有多个，循环插入数据库表中
        for (String tag : tags) {
            int tagId = Integer.parseInt(tag);
            userDao.setLabelByCode(code, rid, tagId);
            row++;
        }
        return row;
    }

    @Override
    public PageBean findArticleByPage(String currentPage_, String rows_, String code) {
        int currentPage = Integer.parseInt(currentPage_);
        int rows = Integer.parseInt(rows_);
        //1.创建一个空对象
        PageBean pb = new PageBean(); //后面也加上了泛型是为了向下兼容

        //2.调用dao查询总记录数
        int totalCount = userDao.findTotalCount(code);

        //3.计算总页码数
        int totalPage = (totalCount % rows) == 0 ? (totalCount / rows) : (totalCount / rows) + 1;
        //4.安全检验,如果当前页小于1,跳转到最后一页,如果当前页大于总页数跳转到第一页
        if (currentPage < 1) {
            currentPage = totalPage; //防止前台用户点击查询当前页的左页导致当前页不存在致使服务器500
        }
        if (currentPage > totalPage) {
            currentPage = 1;
        }
        int start = (currentPage - 1) * rows;
        List<Article> list = userDao.findByPage(start, rows, code);
        pb.setCurrentPage(currentPage);
        pb.setPageSize(rows);
        pb.setTotalCount(totalCount);
        pb.setTotalPage(totalPage);
        pb.setList(list);
        //8.返回PageBean对象
        return pb;
    }

    @Override
    public void deleteArticle(int rid) {
        userDao.deleteOneArticle(rid);
    }

    @Override
    public void delSelectedArticleById(String[] rds) {
        if (rds != null && rds.length > 0) {
            for (int i = 0; i < rds.length; i++) {
                userDao.deleteOneArticle(Integer.parseInt(rds[i]));
            }
        }
    }

    @Override
    public Article findArticleByRid(int rid) {
        return userDao.findArticleByRid(rid);
    }

    @Override
    public void updateArticle(int rid, String title, String text, String modifyTime, String visible) {
        userDao.updateArticle(rid,title,text,modifyTime,visible);
    }

    @Override
    public void deleteArticleFromRecycle(int rid) {
        userDao.deleteArticleFromRecycle(rid);
    }

    @Override
    public PageBean findArticleByPageFromRecycle(String currentPage_, String rows_, String code) {
        int currentPage = Integer.parseInt(currentPage_);
        int rows = Integer.parseInt(rows_);
        PageBean pb = new PageBean(); //后面也加上了泛型是为了向下兼容
        int totalCount = userDao.findTotalCountFromRecycle(code);
        System.out.println("回收站文章总数量：" + totalCount);
        //3.计算总页码数
        int totalPage = (totalCount % rows) == 0 ? (totalCount / rows) : (totalCount / rows) + 1;
        //4.安全检验,如果当前页小于1,跳转到最后一页,如果当前页大于总页数跳转到第一页
        if (currentPage < 1) {
            currentPage = totalPage; //防止前台用户点击查询当前页的左页导致当前页不存在致使服务器500
        }
        if (currentPage > totalPage) {
            currentPage = 1;
        }
        int start = (currentPage - 1) * rows;
        List<Article> list = userDao.findByPageFromRecycle(start, rows, code);
        pb.setCurrentPage(currentPage);
        pb.setPageSize(rows);
        pb.setTotalCount(totalCount);
        pb.setTotalPage(totalPage);
        pb.setList(list);
        return pb;
    }

    @Override
    public void delSelectedArticleFromRecycle(String[] rds) {
        if (rds != null && rds.length > 0) {
            for (int i = 0; i < rds.length; i++) {
                userDao.deleteArticleFromRecycle(Integer.parseInt(rds[i]));
            }
        }
    }

    @Override
    public Favour findLikeOrNot(String code, int rid) {
        return userDao.findLikeOrNot(code,rid);
    }

    @Override
    public void addFavour(String code, int rid) {
        userDao.addFavour(rid);
        int rows = userDao.conformLike(code, rid);
    }

    @Override
    public void updateImage(String code, String image) {
        userDao.updateImageByCode(code,image);
    }

    @Override
    public void recoverArticleByRid(int rid) {
        userDao.recoverArticleByRid(rid);
    }

}
